<template>
  <div>
    <div id="d1" style="margin-top: 200px;border-radius: 10px">
      <h1 style="color: #2c3e50;margin-bottom: 30px;text-align: center;position: relative;left: 35px">管理员登陆</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" style="position: relative;left: 300px">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:9191/admin/login";
          console.log('url=' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log(formData);

          let localJwt = localStorage.getItem("localJwt");
          this.axios.create({
            'headers': {
              'Authorization': localJwt
            }
          }).post(url, formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '登陆成功',
                type: 'success'
              });
              let jwt = jsonResult.data;
              localStorage.setItem("localJwt", jwt);
              console.log(localStorage.getItem("localJwt"),"jwt")
              this.$router.push('/');
            } else {
              this.$message.error(jsonResult.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>
  #d1 {
    width: 800px;
    height: 300px;
    background-color: rgba(0,0,0,0);
    margin: 0px auto;
    padding: 65px 80px 20px 20px;
  }
  body {
    background-image: url("https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA12rZ8l.img");
    background-repeat: no-repeat;
    background-size: cover;
  }
</style>
